<template>
    <div>
        <el-table max-height="510px" :data="adminList" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="240">
            </el-table-column>
            <el-table-column prop="username" label="用户名称" width="150">
            </el-table-column>
            <el-table-column prop="rolename" label="所属角色" width="150">
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status = 1 ? "正常" : "禁用" }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button size="mini" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.uid)" size="mini">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background :page-size="size" layout="prev, pager, next" :total="total"
            @current-change="changePage">
        </el-pagination>
    </div>
</template>

<script>
// 辅助性函数
import { mapGetters, mapActions } from 'vuex'
import { userDel } from '../../request/api'
export default {
    computed: {
        ...mapGetters({
            // 管理员列表
            adminList: 'admin/getAdminList',
            // 总条数
            total: 'admin/getAdminCount',
            // 每页个数
            size: 'admin/getSize'
        })
    },
    mounted() {
        //页面一加载获取管理员列表
        this.getAdminListAction()
        //总条数的获取
        this.getAdminCountAction()
    },
    methods: {
        // 编辑按钮事件
        edit(id) {
            this.$emit('edit', id)
        },

        ...mapActions({
            // 管理员列表
            getAdminListAction: 'admin/getAdminListAction',
            // 总条数
            getAdminCountAction: 'admin/getAdminCountAction',
            // 修改page行动
            changePageAction: 'admin/changePageAction'
        }),
        //封装一个切换页码的事件
        changePage(n) {
            console.log(n, '当前页');
            this.changePageAction(n)
        },
        //封装一个删除事件
        del(uid) {
            this.$confirm("你确定要删除这条数据吗?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    //调用删除接口
                    userDel({
                        uid,
                    }).then((res) => {
                        if (res.code == 200) {
                            this.$message.success(res.msg);
                            //删除成功之后我们要重新调用列表以及总条数
                            this.getAdminListAction()
                            //总条数的获取
                            this.getAdminCountAction()
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
    }
};
</script>

<style  lang="" scoped>

</style>
